/**
 *  JFTSTool - Java Full Text Search tool.
 *  Copyright (C) <2009>  <grupo JFTS>
 *  This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jftstool.semantic;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author grupo JFTS
 *
 */
public class JFTSHierarchyNode {
	private String id = null;
	private int amount = 0;
	private JFTSHierarchyNode superNode = null;
	private List<JFTSHierarchyNode> subNodes = null;
	
	
	public JFTSHierarchyNode(String id){
		this.id = id;
		subNodes = new ArrayList<JFTSHierarchyNode>();
	}
	
	public JFTSHierarchyNode(String id, int amount, List<JFTSHierarchyNode> list) {
		this.id = id;
		this.amount = amount;
		this.subNodes = new ArrayList<JFTSHierarchyNode>();
		
		if (list != null){
			Iterator<JFTSHierarchyNode> it = list.iterator();
			while(it.hasNext()){
				JFTSHierarchyNode nodeAux = it.next();
				nodeAux.setSuperNode(this);
				subNodes.add(nodeAux);
			}
		}
	}

	public JFTSHierarchyNode getSuperNode(){
		return superNode;
	}
	
	public Iterator<JFTSHierarchyNode> listSubNodes(){
		return subNodes.iterator();
	}
	
	protected void setSuperNode(JFTSHierarchyNode node){
		this.superNode = node;
	}
	
	protected void addSubNode(JFTSHierarchyNode node){
		node.setSuperNode(this);		
		subNodes.add(node);
	}
	
	public String getId(){
		return id;
	}
	
	public int getAmount(){
		return amount;
	}	
	
	public String getPath(){
		JFTSHierarchyNode father = this.superNode;
		if (father == null){
			return id;
		}
		else{
			return father.getPath() + "/" + id;
		}
	}
	
	public String getPath(char separator){
		JFTSHierarchyNode father = this.superNode;
		if (father == null){
			return id;
		}
		else{
			return father.getPath(separator) + separator + id;
		}
	}
	
	public boolean equals(Object o){
		return this == o || this.id.equals(((JFTSHierarchyNode) o).getId()); 
	}
}
